#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setupUi();
    connectSignalsSlots();
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::setupUi() {
    // 设置主窗口的属性和布局
    this->setWindowTitle("Customer Management System");
    this->resize(400, 300);

    // 创建和放置UI组件
    editName = new QLineEdit(this);
    editEmail = new QLineEdit(this);
    editId = new QLineEdit(this);
    btnAddCustomer = new QPushButton("Add Customer", this);
    btnRemoveCustomer = new QPushButton("Remove Customer", this);
    btnUpdateCustomer = new QPushButton("Update Customer", this);
    btnShowAllCustomers = new QPushButton("Show All Customers", this);
    labelStatus = new QLabel("Ready", this);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(new QLabel("Name:"), 0, 0);
    layout->addWidget(editName, 0, 1);
    layout->addWidget(new QLabel("Email:"), 1, 0);
    layout->addWidget(editEmail, 1, 1);
    layout->addWidget(new QLabel("ID (for Remove/Update):"), 2, 0);
    layout->addWidget(editId, 2, 1);
    layout->addWidget(btnAddCustomer, 3, 0, 1, 2);
    layout->addWidget(btnRemoveCustomer, 4, 0, 1, 2);
    layout->addWidget(btnUpdateCustomer, 5, 0, 1, 2);
    layout->addWidget(labelStatus, 6, 0, 1, 2);
    layout->addWidget(btnShowAllCustomers, 7, 0, 1, 2);


    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    this->setCentralWidget(centralWidget);
}

void MainWindow::connectSignalsSlots() {
    connect(btnAddCustomer, &QPushButton::clicked, this, &MainWindow::onAddCustomerClicked);
    connect(btnRemoveCustomer, &QPushButton::clicked, this, &MainWindow::onRemoveCustomerClicked);
    connect(btnUpdateCustomer, &QPushButton::clicked, this, &MainWindow::onUpdateCustomerClicked);
    connect(btnShowAllCustomers, &QPushButton::clicked, this, &MainWindow::onShowAllCustomersClicked);

}

void MainWindow::onAddCustomerClicked() {
    bool result = customerManager.addCustomer(editName->text(), editEmail->text());
    if (result) {
        labelStatus->setText("Customer added successfully.");
        clearInputFields();
    } else {
        labelStatus->setText("Failed to add customer.");
    }
}

void MainWindow::onRemoveCustomerClicked() {
    bool result = customerManager.removeCustomer(editId->text().toInt());
    if (result) {
        labelStatus->setText("Customer removed successfully.");
        clearInputFields();
    } else {
        labelStatus->setText("Failed to remove customer.");
    }
}

void MainWindow::onUpdateCustomerClicked() {
    bool result = customerManager.updateCustomer(editId->text().toInt(), editName->text(), editEmail->text());
    if (result) {
        labelStatus->setText("Customer updated successfully.");
        clearInputFields();
    } else {
        labelStatus->setText("Failed to update customer.");
    }
}
void MainWindow::onShowAllCustomersClicked() {
    QList<QPair<int, QString>> customers = customerManager.getAllCustomers();
    QString info;
    for (const auto &customer : customers) {
        info += QString("ID: %1, Name: %2\n").arg(customer.first).arg(customer.second);
    }
    QMessageBox::information(this, "All Customers", info.isEmpty() ? "No customers found." : info);
}

void MainWindow::clearInputFields() {
    editName->clear();
    editEmail->clear();
    editId->clear();
}